﻿using Swashbuckle.Application;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Cors;
using WebApplication1.Auth.CustomAuth;

namespace WebApplication1
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            #region 允许全局跨域

            var cors = new EnableCorsAttribute("*", "*", "*");
            cors.PreflightMaxAge = 100000;
            //跨域
            config.EnableCors(cors);

            #endregion


            // Web API 配置和服务

            // Web API 路由

            // 启用属性路由
            config.MapHttpAttributeRoutes();

            config.Filters.Add(new ApiJWTAuthorizeAttribute());

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            #region 设置默认的为Swagger
                     
            config.Routes.MapHttpRoute(
                name: "swagger_root",
                routeTemplate: "",
                defaults: null,
                constraints: null,
                handler: new RedirectHandler((message => message.RequestUri.ToString()), "swagger"));

            #endregion

            #region 注释部分

            //swagger / ui / index
            //config.Routes.MapHttpRoute(
            //    name: "ActionApi",
            //    routeTemplate: "api/{controller}/{action}/{id}",
            //    defaults: new { id = RouteParameter.Optional }
            //);

            //routes.MapRoute(
            //    name: "Default",
            //    url: "{controller}/{action}/{id}",
            //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            //);

            #endregion
        }
    }
}
